// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init({
  env: cloud.DYNAMIC_CURRENT_ENV
})

const db = cloud.database();
const _ = db.command;

// 云函数入口函数
exports.main = async (event, context) => {
  try {
    const { page = 1, pageSize = 10,  searchKey = '' } = event;
    const skip = (page - 1) * pageSize;
    
    // 构建查询条件
    const query = {
      'ipId': 'other' ,
      status:true
    };
    
    // 添加搜索条件
    if (searchKey) {
      query.goodsName = db.RegExp({
        regexp: searchKey,
        options: 'i' // 不区分大小写
      });
    }
    
    
    // 执行查询
    const goodsResult = await db.collection('blindbox_goods')
      .where(query)
      .orderBy('createTime', 'desc')
      .skip(skip)
      .limit(pageSize)
      .get();
    
    // 获取总数量
    const countResult = await db.collection('blindbox_goods')
      .where(query)
      .count();
    
    return {
      success: true,
      data: goodsResult.data,
      total: countResult.total
    };
  } catch (e) {
    console.error('获取其他商品列表失败', e);
    return {
      success: false,
      message: '获取其他商品列表失败',
      error: e
    };
  }
};